// pages/voucher/voucher.js
const app = getApp();
Page({
  data: {
    windowHeight:'',
    animationData: {},
    voucherList: [],
    current:1,
    dataList: [],
    noData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/vouchers/findlist?page=0&&size=10',
      method: 'POST',
      header: {
        token: app.globalData.token,
        version: app.globalData.version
      },
      data: {
        accountBookId: app.globalData.accountBookId,
        endPeriod: options.data
      },
      success: function (res) {
        if(res.data.vouchers.content.length!==0){
          that.data.noData = false;
          that.data.voucherList = res.data.vouchers.content;
          that.handleData();
        }else{
          wx.hideLoading()
          that.data.noData = true
        }
        that.setData({
          noData: that.data.noData
        })
      }
    })

    //设置NavigationBarTitle
    wx.setNavigationBarTitle({
      title: `凭证预览`
    })
    
    let windowHeight = app.globalData.windowHeight;
    let windowWidth = app.globalData.windowWidth;

    if (app.globalData.model == 'iPhone X'){
      windowHeight = app.globalData.windowHeight-24
    }
    // 添加动画，使凭证旋转（竖着排列）
    var animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1,
    });
    animation.scale(1.3, windowHeight / windowWidth*0.90).rotate(90).translate((windowHeight-65)/4.7).step();
    this.setData({
      animationData: animation.export()
    })
  },

  handleData(){

    for (let i = 0; i < this.data.voucherList.length; i++) {
      let num = Math.ceil(this.data.voucherList[i].voucherRecords.length / 5)
      for (let j = 0; j < num; j++) {
        this.data.dataList.push({
          "dftotal": this.data.voucherList[i].dftotal,
          "jftotal": this.data.voucherList[i].jftotal,
          "voucherDateStr": this.data.voucherList[i].voucherDateStr,
          "voucherNo": this.data.voucherList[i].voucherNo,
          "writtenPerson": this.data.voucherList[i].writtenPerson,
          "num": num,
          "currentNum": j + 1,
          "totalDX": this.DX(this.data.voucherList[i].dftotal),
          "voucherRecords": [
            this.data.voucherList[i].voucherRecords[0 + 5 * j],
            this.data.voucherList[i].voucherRecords[1 + 5 * j],
            this.data.voucherList[i].voucherRecords[2 + 5 * j],
            this.data.voucherList[i].voucherRecords[3 + 5 * j],
            this.data.voucherList[i].voucherRecords[4 + 5 * j]
          ]
        })
      }
    }

    this.setData({
      voucherList: this.data.voucherList,
      windowHeight: app.globalData.windowHeight,
      dataList: this.data.dataList
    })

    wx.hideLoading()
  },
  // 中文大写（处理数据中的合计金额）
  DX(n) {
    var unit = "仟佰拾亿仟佰拾万仟佰拾元角分", str = "";
    n += "00";
    var p = n.indexOf('.');
    if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
    unit = unit.substr(unit.length - n.length);
    for (var i = 0; i < n.length; i++)
      str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
      if (String(n).indexOf('-') == -1) {
        if (/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
          return str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
        } else {
          if (n == '000') return '零元整'
          else {
            let newStr = str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
            return newStr;
          }
        }
      } else {
        let newStr = str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"),
        need = newStr.slice(1),
        show = '负'.concat(need);
        return show;
      }
  },

  // 滑动修改navigationBarTitle
  setNavigationBarTitle: function (e) {
    if (e.detail.source==='touch'){
      let current = e.detail.current + 1;
      this.setData({
        current: current,
      })
    }
  },
  prePage(){
    if (this.data.current==1) return
    let current = this.data.current - 1;
    this.setData({
      current: current
    })
  },
  nextPage(){
    if (this.data.current == this.data.dataList.length) return
    let current = this.data.current+1;
    this.setData({
      current: current
    })
  }
})